//路由
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../views/Layout/Layout.vue'
import HomePage from '../views/Layout/HomePage.vue'
import ArticleManage from '@/views/ArticleManage.vue'
import ArticlesPage from '@/views/ArticlesPage.vue'
import ArticleDetail from '@/views/ArticleDetail.vue'
import { useUserStore } from '@/stores/user'

const routes = [
  {
    path: '/booking',
    name: 'Booking',
    component: () => import('@/views/BookingPage.vue'),
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'home',
        meta: { show: true },
        component: HomePage,
      },
      {
        path: 'Book',
        name: 'Book',
        component: () => import('@/views/BookingPage.vue'),
      },
      {
        path: '/articles',
        children: [
          {
            path: '',
            component: ArticlesPage,
          },
          {
            path: 'manage',
            component: ArticleManage,
          },
          {
            path: ':id',
            component: ArticleDetail,
          },
        ],
      },
      {
        path: 'articles/:id',
        name: 'article-detail',
        component: () => import('@/views/ArticleDetail.vue'),
        beforeEnter: (to, from, next) => {
          console.log('进入文章详情页面，文章 ID:', to.params.id)
          next()
        },
      },
      {
        path: 'tests',
        name: 'tests',
        component: () => import('../views/tests.vue'),
      },
      {
        path: 'test/:id',
        name: 'test-detail',
        component: () => import('../views/test-detail.vue'),
      },
      {
        path: 'login',
        name: 'login',
        component: () => import('@/views/LoginPage.vue'),
      },
      {
        path: 'register',
        name: 'register',
        component: () => import('@/views/RegisterPage.vue'),
      },
      {
        path: 'user-management',
        name: 'UserManagement',
        component: () => import('@/views/UserManagement.vue'), // 用户管理页面
        meta: { requiresAdmin: true }, // 仅管理员可访问
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('@/views/UserProfile.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'profile/basic',
        name: 'BasicInfoEdit',
        component: () => import('@/views/profile/BasicInfoEdit.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'profile/evaluations',
        name: 'EvaluationResults',
        component: () => import('@/views/profile/EvaluationResults.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'profile/evaluation/:id',
        name: 'EvaluationDetail',
        component: () => import('@/views/profile/EvaluationDetail.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'profile/appointments',
        name: 'Appointments',
        component: () => import('@/views/profile/Appointments.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: 'profile/book-room',
        name: 'BookRoom',
        component: () => import('@/views/profile/BookRoom.vue'),
        meta: { requiresAuth: true, requiresAssistant: true },
      },
      {
        path: 'profile/user-all-evaluation-reports',
        name: 'UserAllEvaluationReports',
        component: () => import('@/views/profile/UserAllEvaluationReports.vue'),
        meta: { requiresAuth: true, requiresCounselor: true },
      },
      {
        path: 'test/manage',
        name: 'TestManage',
        component: () => import('@/views/TestManage.vue'),
        meta: {
          requiresAuth: true,
          roles: ['admin', 'counselor'],
        },
      },
      {
        path: 'counselor/:id',
        name: 'CounselorDetail',
        component: () => import('@/views/counselor/CounselorDetail.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 检查是否需要登录
  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
    return
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && !userStore.user?.is_admin) {
    next('/')
    return
  }

  // 检查是否需要咨询师权限
  if (to.meta.requiresCounselor && !userStore.user?.is_counselor) {
    next('/')
    return
  }

  next()
})

export default router
